package rz.webservice;

import java.net.MalformedURLException;
import java.rmi.RemoteException;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.encoding.TypeMapping;
import javax.xml.rpc.encoding.TypeMappingRegistry;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.encoding.ser.BeanDeserializerFactory;
import org.apache.axis.encoding.ser.BeanSerializerFactory;

import com.Student;

public class WeatherTest {

	protected static void registerBeanMapping(TypeMapping mapping, Class type,  
            javax.xml.namespace.QName qname) {  
        mapping.register(type, qname, new BeanSerializerFactory(type, qname),  
        new BeanDeserializerFactory(type, qname));  
    }  
	
	/**
	 * 2011-3-18 下午02:15:40
	 * @author
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			 try {

			        String endpoint = "http://localhost:8081/weebs/services/ServiceMethod"; 

			        Service  service = new Service();

			        Call call = (Call) service.createCall();  

			        call.setTargetEndpointAddress( new java.net.URL(endpoint) );

			        call.setOperationName(new QName("http://ddh.com", "checkUserRight")  );

			        call.addParameter( "userId", XMLType.XSD_STRING, ParameterMode.IN );        
			        call.addParameter( "password", XMLType.XSD_STRING, ParameterMode.IN );        

			        call.setReturnType( XMLType.XSD_ANYTYPE );
			        
			        QName qn = new QName("http://bean.ddh.com/xsd", "Student");
			        call.registerTypeMapping(Student.class, qn, new BeanSerializerFactory(Student.class, qn), new BeanDeserializerFactory(Student.class, qn));

			        Student ret = (Student) call.invoke( new Object [] { "fred", "" });        

			        System.out.println("Result : " + ret.getAge()); 

			    } catch (Exception e) {

			        System.err.println(e.toString());

			     }    
			
			System.out.println("******************************88");
			/*Service service = new Service();
			
			Call call = null;
			call = (Call) service.createCall();
			call.setOperationName(new QName("http://www.xtdclub.com/Service/weather.asmx", "GetStringWeather"));
			call.setTargetEndpointAddress(new java.net.URL("http://www.4599.cc/service/weather.asmx"));
			// 传入参数
			call.addParameter(new QName("http://www.xtdclub.com/Service/weather.asmx", "City"),
					org.apache.axis.Constants.XSD_STRING, ParameterMode.IN);
			call.setReturnType(org.apache.axis.Constants.XSD_STRING);
			call.setSOAPVersion(org.apache.axis.soap.SOAPConstants.SOAP12_CONSTANTS);

			Object result = call.invoke(new Object[] {"杭州"});
			
			System.out.println(result);*/
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	

	}
	
	
	public static void webxml() {
		try {
			
			Service service = new Service();
			Call call = null;
			call = (Call) service.createCall();
			call.setOperationName(new QName("http://WebXml.com.cn/", "getMobileCodeInfo "));
			call.setTargetEndpointAddress(new java.net.URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"));
			// 传入参数
			call.addParameter(new QName("http://WebXml.com.cn/", "mobileCode"),
					org.apache.axis.Constants.XSD_STRING, ParameterMode.IN);
			call.addParameter(new QName("http://WebXml.com.cn/", "userID"),
					org.apache.axis.Constants.XSD_STRING, ParameterMode.IN);
			call.setReturnType(org.apache.axis.Constants.XSD_STRING);
			call
					.setSOAPVersion(org.apache.axis.soap.SOAPConstants.SOAP12_CONSTANTS);

			String result = (String) call.invoke(new Object[] { "1357578", "" });
			System.out.println(result);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (ServiceException e) {
			e.printStackTrace();
		}
	}

}
